﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace QLKS
{
    public partial class QLNV : Form
    {
        public f_Login flogin = new f_Login();
        public Dictionary<string, string> GetPassFromUser;
        public Mysql qmysql = new Mysql();
        public QLNV()
        {
            InitializeComponent();
        }

        private void QLNV_Load(object sender, EventArgs e)
        {
            //Getdata from database.
            string cmdText = "SELECT MaNV, Username, TenNV, CMND, Diachi, Dienthoai, ht_chucvu.TenCV FROM ht_nhanvien, ht_chucvu WHERE ht_nhanvien.MaCV = ht_chucvu.MaCV";
            Dictionary<string, string> Params = new Dictionary<string, string>();
            DataTable GetAllNhanVien = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
            grv_nv.DataSource = GetAllNhanVien;

            //
            cmdText = "SELECT * FROM ht_chucvu";
            Dictionary<string, string> Params_cv = new Dictionary<string, string>();
            DataTable GetAllChucVu = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params_cv);
            cb_chucvu.DataSource = GetAllChucVu;
            cb_chucvu.DisplayMember = "TenCV";
            cb_chucvu.ValueMember = "MaCV";
        }

        private void grv_nv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            tx_manv.Text = grv_nv.Rows[e.RowIndex].Cells["MaNV"].Value.ToString();
            tx_tennv.Text = grv_nv.Rows[e.RowIndex].Cells["TenNV"].Value.ToString();
            tx_username.Text = grv_nv.Rows[e.RowIndex].Cells["Username"].Value.ToString();
            tx_cmnd.Text = grv_nv.Rows[e.RowIndex].Cells["CMND"].Value.ToString();
            tx_diachi.Text = grv_nv.Rows[e.RowIndex].Cells["Diachi"].Value.ToString();
            tx_dienthoai.Text = grv_nv.Rows[e.RowIndex].Cells["Dienthoai"].Value.ToString();

            cb_chucvu.SelectedIndex = cb_chucvu.FindStringExact(grv_nv.Rows[e.RowIndex].Cells["TenCV"].Value.ToString());
        }

        private void bt_nvthem_Click(object sender, EventArgs e)
        {
            int error = 0;
            if (error==0 && (tx_matkhau.Text=="" || tx_tennv.Text == "" || tx_username.Text == "" || tx_cmnd.Text == "" || tx_diachi.Text == "" || tx_dienthoai.Text == ""))
            {
                MessageBox.Show("Không để trống thông tin trong các mục có dấu *", "Thông Báo");
                error = 1;
            }

            //get data
            string tmp_matkhau = Program.md5(LocChuoi(tx_matkhau.Text));
            string tmp_tenv = LocChuoi(tx_tennv.Text);
            string tmp_username = LocChuoi(tx_username.Text);
            string tmp_cmnd = LocChuoi(tx_cmnd.Text);
            string tmp_diachi = LocChuoi(tx_diachi.Text);
            string tmp_dienthoai = LocChuoi(tx_dienthoai.Text);
            string tmp_chucvu = LocChuoi(cb_chucvu.SelectedValue.ToString());

            if (error == 0)
            {
                //Getdata from database.
                string cmdTextCdta = "SELECT * FROM ht_nhanvien WHERE Username=?user OR CMND=?cmnd LIMIT 0,1";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                Params["user"] = tmp_username;
                Params["cmnd"] = tmp_cmnd;
                Dictionary<string, string> GetInfoFromUser = qmysql.SelectReaderSqlCommand(cmdTextCdta, Params);
                if (GetInfoFromUser.Count > 0)
                {
                    MessageBox.Show("Username hoặc CMND đã tồn tại trong hệ thống", "Thông Báo");
                    error = 1;
                }
            }

            //add data
            if (error == 0)
            {
                string cmdTextIsnv = "INSERT INTO ht_nhanvien(TenNV,Username,Matkhau,Dienthoai,CMND,Diachi,MaCV) VALUES(?tennv,?user,?mk,?dt,?cmnd,?dc,?macv)";
                Dictionary<string, string> Params_isnv = new Dictionary<string, string>();
                Params_isnv["tennv"] = tmp_tenv;
                Params_isnv["user"] = tmp_username;
                Params_isnv["mk"] = tmp_matkhau;
                Params_isnv["dt"] = tmp_dienthoai;
                Params_isnv["cmnd"] = tmp_cmnd;
                Params_isnv["dc"] = tmp_diachi;
                Params_isnv["macv"] = tmp_chucvu;
                this.qmysql.NonQueryMysqlCommand(cmdTextIsnv, Params_isnv);
                MessageBox.Show("Đã thêm tài khoản thành công", "Thông Báo");
                //reload data
                //Getdata from database.
                //Getdata from database.
                string cmdText = "SELECT MaNV, Username, TenNV, CMND, Diachi, Dienthoai, ht_chucvu.TenCV FROM ht_nhanvien, ht_chucvu WHERE ht_nhanvien.MaCV = ht_chucvu.MaCV";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                DataTable GetAllNhanVien = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
                grv_nv.DataSource = GetAllNhanVien;
                grv_nv.Refresh();
            }
        }

        public string LocChuoi(string strText)
        {
            return Regex.Replace(strText.Trim(), @"\W\s+", "");
        }

        private void bt_suanv_Click(object sender, EventArgs e)
        {
            int error = 0;
            if (error == 0 && (tx_manv.Text == "" || tx_tennv.Text == "" || tx_username.Text == "" || tx_cmnd.Text == "" || tx_diachi.Text == "" || tx_dienthoai.Text == ""))
            {
                MessageBox.Show("Không để trống thông tin trong các mục có dấu *", "Thông Báo");
                error = 1;
            }

            //get data
            string tmp_manv = tx_manv.Text;
            string tmp_matkhau = "";
            if (tx_matkhau.Text != "")
            {
                tmp_matkhau = Program.md5(LocChuoi(tx_matkhau.Text));
            }
            string tmp_tenv = LocChuoi(tx_tennv.Text);
            string tmp_username = LocChuoi(tx_username.Text);
            string tmp_cmnd = LocChuoi(tx_cmnd.Text);
            string tmp_diachi = LocChuoi(tx_diachi.Text);
            string tmp_dienthoai = LocChuoi(tx_dienthoai.Text);
            string tmp_chucvu = LocChuoi(cb_chucvu.SelectedValue.ToString());

            if (error == 0)
            {
                //Getdata from database.
                string cmdTextCdta = "SELECT * FROM ht_nhanvien WHERE Username=?user OR CMND=?cmnd LIMIT 0,1";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                Params["user"] = tmp_username;
                Params["cmnd"] = tmp_cmnd;
                Dictionary<string, string> GetInfoFromUser = qmysql.SelectReaderSqlCommand(cmdTextCdta, Params);
                if (GetInfoFromUser.Count > 9)
                {
                    MessageBox.Show("Username hoặc CMND đã tồn tại trong hệ thống", "Thông Báo");
                    error = 1;
                }
            }

            //add data
            if (error == 0)
            {
                string cmdTextIsnv ="";
                if (tx_matkhau.Text != "")
                {
                    cmdTextIsnv = "UPDATE ht_nhanvien SET TenNV=?tennv,Username=?user,Matkhau=?mk,Dienthoai=?dt,CMND=?cmnd,Diachi=?dc,MaCV=?macv WHERE MaNV=?manv";
                }
                else
                {
                    cmdTextIsnv = "UPDATE ht_nhanvien SET TenNV=?tennv,Username=?user,Dienthoai=?dt,CMND=?cmnd,Diachi=?dc,MaCV=?macv WHERE MaNV=?manv";
                }
                Dictionary<string, string> Params_isnv = new Dictionary<string, string>();
                Params_isnv["tennv"] = tmp_tenv;
                Params_isnv["user"] = tmp_username;
                if (tx_matkhau.Text != "")
                {
                    Params_isnv["mk"] = tmp_matkhau;
                }
                Params_isnv["dt"] = tmp_dienthoai;
                Params_isnv["cmnd"] = tmp_cmnd;
                Params_isnv["dc"] = tmp_diachi;
                Params_isnv["macv"] = tmp_chucvu;
                Params_isnv["manv"] = tmp_manv;
                this.qmysql.NonQueryMysqlCommand(cmdTextIsnv, Params_isnv);
                MessageBox.Show("Đã sửa tài khoản thành công", "Thông Báo");
                //reload data
                //Getdata from database.
                //Getdata from database.
                string cmdText = "SELECT MaNV, Username, TenNV, CMND, Diachi, Dienthoai, ht_chucvu.TenCV FROM ht_nhanvien, ht_chucvu WHERE ht_nhanvien.MaCV = ht_chucvu.MaCV";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                DataTable GetAllNhanVien = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
                grv_nv.DataSource = GetAllNhanVien;
                grv_nv.Refresh();
            }
        }

        private void bt_xoanv_Click(object sender, EventArgs e)
        {
            int error = 0;
            if (error == 0 && tx_manv.Text == "")
            {
                MessageBox.Show("Không nhận diện được thông tin để xóa tài khoản", "Thông Báo");
                error = 1;
            }

            if (error == 0)
            {
                DialogResult plv = MessageBox.Show("Bạn có chắc chắn thực hiện thao tác này không ?", "Xóa Tài Khoản", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (plv.ToString() == "No")
                {
                    error = 1;
                }
            }

            //get data
            string tmp_manv = tx_manv.Text;

            //add data
            if (error == 0)
            {
                string cmdTextIsnv  = "DELETE FROM ht_nhanvien WHERE MaNV=?manv";
                Dictionary<string, string> Params_isnv = new Dictionary<string, string>();
                Params_isnv["manv"] = tmp_manv;
                this.qmysql.NonQueryMysqlCommand(cmdTextIsnv, Params_isnv);
                MessageBox.Show("Đã xóa tài khoản thành công", "Thông Báo");
                //reload data
                //Getdata from database.
                //Getdata from database.
                string cmdText = "SELECT MaNV, Username, TenNV, CMND, Diachi, Dienthoai, ht_chucvu.TenCV FROM ht_nhanvien, ht_chucvu WHERE ht_nhanvien.MaCV = ht_chucvu.MaCV";
                Dictionary<string, string> Params = new Dictionary<string, string>();
                DataTable GetAllNhanVien = qmysql.SelectReaderSqlCommandDatatable(cmdText, Params);
                grv_nv.DataSource = GetAllNhanVien;
                grv_nv.Refresh();
            }
        }
    }
}
